/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Xceed.Silverlight.Data
{
  public sealed class ItemChangedDescription
  {
    #region Constructors

    public ItemChangedDescription( object item )
    {
      if( item == null )
        throw new ArgumentNullException( "item" );

      this.Item = item;
      m_readOnlyChangedProperties = new ReadOnlyCollection<PropertyChangedDescription>( m_changedProperties );
    }

    public ItemChangedDescription( object item, string propertyName )
      : this( item, new string[] { propertyName } )
    {
    }

    public ItemChangedDescription( object item, string propertyName, object oldValue )
      : this( item, new PropertyChangedDescription[] { new PropertyChangedDescription( propertyName, oldValue ) } )
    {
    }

    public ItemChangedDescription( object item, IEnumerable<string> propertiesName )
      : this( item, ItemChangedDescription.CreateDescriptions( propertiesName ) )
    {
    }

    public ItemChangedDescription( object item, IEnumerable<PropertyChangedDescription> propertiesChanged )
      : this( item )
    {
      if( propertiesChanged == null )
      {
        propertiesChanged = new PropertyChangedDescription[ 0 ];
      }

      PropertyChangedDescription[] propertyChangedDescriptions = propertiesChanged.ToArray();

      if( propertyChangedDescriptions.Length > 0 )
      {
        if( ItemChangedDescription.HasDuplicateValues( propertyChangedDescriptions ) )
          throw new ArgumentException( "The specified property must be unique.", "propertiesChanged" );

        m_changedProperties.AddRange( propertyChangedDescriptions );
      }
    }

    #endregion

    #region Item Property

    public object Item
    {
      get;
      private set;
    }

    #endregion

    #region ChangedProperties Property

    public ICollection<PropertyChangedDescription> ChangedProperties
    {
      get
      {
        return m_readOnlyChangedProperties;
      }
    }

    private List<PropertyChangedDescription> m_changedProperties = new List<PropertyChangedDescription>( 0 );
    private ReadOnlyCollection<PropertyChangedDescription> m_readOnlyChangedProperties;

    #endregion

    internal ItemChangedDescription CreateWithNewItem( object item )
    {
      if( item == null )
        throw new ArgumentNullException( "item" );

      return new ItemChangedDescription( item, m_readOnlyChangedProperties );
    }

    private static IEnumerable<PropertyChangedDescription> CreateDescriptions( IEnumerable<string> propertiesNames )
    {
      if( propertiesNames == null )
        return Enumerable.Empty<PropertyChangedDescription>();

      return propertiesNames.Select( ( propertyName ) => new PropertyChangedDescription( propertyName ) );
    }

    private static bool HasDuplicateValues( IEnumerable<PropertyChangedDescription> propertiesChanged )
    {
      if( ( propertiesChanged != null ) && ( propertiesChanged.Any() ) )
      {
        HashSet<string> distinctNames = new HashSet<string>();

        foreach( PropertyChangedDescription propertyChanged in propertiesChanged )
        {
          string propertyName = propertyChanged.PropertyName;

          if( distinctNames.Contains( propertyName ) )
            return true;

          distinctNames.Add( propertyName );
        }
      }

      return false;
    }
  }
}
